function writeTree(struct,wtable,state) {

	this.struct=struct;
	this.table=document.getElementById(wtable).
			getElementsByTagName('tbody').item(0);

	if (!this.table) {

		this.table=document.createElement('tbody');
		document.getElementById(wtable).appendChild(this.table);
	}

	this.items={};
	this.children={};
	this.expanded={};
	this.sid=0;
	this.wtable=wtable;
	//if (state) this.tree=eval(state);
	if (state) this.a=state.split(":");
	this.row=0;
	this.level=-1;

	this.init=function() {

		if (this.a) for (var i=0;i<this.a.length;i++)
				this.expanded[this.a[i]]=true;

		for (var i in this.struct) {
		//for (var i=0;i<this.struct.length;i++){

			if(this.struct[i]){

				var id=this.struct[i][0];
				var pt=this.struct[i][1];

				if(!this.children[pt]) this.children[pt]=[];
				this.items[id]=this.struct[i];
				this.children[pt].push(id);
			}
		}

		var i=this.sid;

		while (this.items[i]) {

			this.expanded[i]=true;
			i=this.items[i][1];
		}

		this.save_expanded();
	}

	this.save_expanded=function() {

		var s='';

		for (var i in this.expanded)

			if(this.expanded[i]==true)s+=i+":";

		this.save=eval('document.navigation.'+this.wtable+'_setting')

		if (this.save) this.save.value=s;
	}

	this.reset_rows=function(pt) {

		if(this.children[pt] && this.expanded[pt]){

			//for (var i=0; i<this.children[pt].length; i++) {
			for (var i in this.children[pt]) {

				this.row++;
				this.level++;
				var j=this.children[pt][i];
				var trprop=this.items[j][3];
				var tr=document.getElementById('item_'+trprop.toggleid+j);

				var style;
				this.row%2?style='row_a':style='row_b';
				tp=trprop.onmouseout.replace('{class}',trprop[style]);
				tr.className=trprop[style];
				tr.firstChild.className=trprop.tree_level+this.level;
				eval('tr.onmouseout=function() { this.className=\''+trprop[style]+'\'; }');
				if (this.children[j]) this.reset_rows(j);
				this.level--;
			}
		}
	}

	this.list=function(pt){

		if(this.children[pt] && this.expanded[pt]){

			var iobj,obj,pos;

			//for (var i=0; i<this.children[pt].length; i++){
			for (var i in this.children[pt]) {

				var j = this.children[pt][i];
				var s;
				this.row++;
				this.level++;
				var tv=(this.expanded[j]?'toggle_open':'toggle_close');
				if (this.children[j]) s='toggle';
				else s='item';

				var tr=document.createElement('tr');
				var trprop=this.items[j][3];
				var style;
				this.row%2?style='row_a':style='row_b';

				for (var t in trprop) {

					if (t == 'onmouseover') {

						tp=trprop[t].replace('{class}',trprop['row_c']);
						eval('tr.onmouseover=function() { '+tp+' }');
					} else if (t == 'onmouseout') {

						tp=trprop[t].replace('{class}',trprop[style]);
						eval('tr.onmouseout=function() { '+tp+' }');
					} else if (t == 'onclick') {

						eval('tr.onclick=function() { '+trprop[t]+' }');
					} else {

						tp=trprop[t];
						//tr.setAttribute(t,tp);
					}
				}

				tr.setAttribute('id','item_'+trprop.toggleid+j);
				tr.className=trprop[style];
				var cells=this.items[j][2];

				for (var cs=0;cs<cells.length;cs++) {

					var cell=cells[cs];
					var td=document.createElement('td');

					for (var c in cell) {

						if (c=='value') continue;
						if (c=='className') td.className=cell[c];
						else td.setAttribute(c,cell[c]);
					}

					if (cs && cell) td.innerHTML=cell.value;
					else if (!cs) {
					//else {
					
						td.innerHTML=trprop[s].
						replace('{class}',
						trprop[tv]);
						td.className=trprop.tree_level+this.level;
					}

					tr.appendChild(td);
				}

				if (!obj) {

					if (obj=document.getElementById('item_'+trprop.toggleid+pt))

						pos=obj.nextSibling;
					else if (!this.level) {

						iobj=null;
					} else {

						iobj=this.table.lastChild;
					}
				}

				if (obj) {

					this.table.insertBefore(tr,pos);
				} else {

					this.table.insertBefore(tr,iobj);
				}

				if (this.children[j]) this.list(j);
				this.level--;
			}
		}
	}

	this.toggleNode=function(id){

		var trprop=this.items[id][3];
		var s=document.getElementById(trprop.toggleid+id);

		if (this.expanded[id]) {

			this.expanded[id]=false;
			s.className=trprop.toggle_close;
			this.hideChildren(id);
		} else {

			this.expanded[id] = true;
			s.className=trprop.toggle_open;
			this.list(id);
		}

		this.row=0;
		this.level=-1;
		this.reset_rows(this.row);
		this.save_expanded();
	}

	this.hideChildren=function(id) {

		if (!this.children[id]) return(false);

		//for (var i=0;i<this.children[id].length;i++) {
		for (var i in this.children[id]) {

			var c=document.getElementById
					('item_'+this.items[id][3].toggleid+
					this.children[id][i]);

			if (c) {

				this.hideChildren(
					this.items[this.children[id][i]][0]);
				this.table.removeChild(c);
			}
		}
	}

	this.init();
	this.expanded[0] = true;
	this.list(0);
}

